package cn.jiyun.service.impl;

import cn.jiyun.entity.XmAuditbusiness;
import cn.jiyun.entity.XmOrderGoodslist;
import cn.jiyun.entity.XmUser;
import cn.jiyun.mapper.*;
import cn.jiyun.service.IXmUserService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.sql.Array;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-11-21
 */
@Service
public class XmUserServiceImpl extends ServiceImpl<XmUserMapper, XmUser> implements IXmUserService {


    @Override
    public Page<XmUser> findAll(Integer current, Integer pageSize, XmUser xmUser) {
        Page<XmUser> xmUserPage = new Page<>(current, pageSize);
        QueryWrapper<XmUser> xmUserQueryWrapper = new QueryWrapper<>();
        if (xmUser.getId() != null) {
            xmUserQueryWrapper.like("id", xmUser.getId());
        }
        if (xmUser.getName() != null && !xmUser.getName().equals("")) {
            xmUserQueryWrapper.like("name", xmUser.getName());
        }
        if (xmUser.getZname() != null && !xmUser.getZname().equals("")) {
            xmUserQueryWrapper.like("zname", xmUser.getZname());
        }
        if (xmUser.getMobilePhone() != null && !xmUser.getMobilePhone().equals("")) {
            xmUserQueryWrapper.like("mobile_phone", xmUser.getMobilePhone());
        }
        if (xmUser.getElectronicMailbox() != null && !xmUser.getElectronicMailbox().equals("")) {
            xmUserQueryWrapper.like("electronic_mailbox", xmUser.getElectronicMailbox());
        }
        if (xmUser.getIdentity() != null) {
            xmUserQueryWrapper.eq("identity", xmUser.getIdentity());
        }
        if (xmUser.getZtime() != null) {
            LocalDateTime now = LocalDateTime.now();
            int mo = 90;
            if (xmUser.getZtime() == 1) {
              xmUserQueryWrapper.apply("ABS(DATEDIFF({0}, registration)) <= {1}", now, mo);
            }

            if(xmUser.getZtime()==2){
                xmUserQueryWrapper.apply("YEAR(registration)=YEAR(CURRENT_DATE)");
            }

            if(xmUser.getZtime()==3){
                xmUserQueryWrapper.apply("DATE_FORMAT(registration,'%Y')=2022");
            }

            if(xmUser.getZtime()==4){
                 xmUserQueryWrapper.apply("DATE_FORMAT(registration,'%Y')=2021");
            }
             if(xmUser.getZtime()==5){
                 xmUserQueryWrapper.apply("DATE_FORMAT(registration,'%Y')=2020");
            }
              if(xmUser.getZtime()==6){
                 xmUserQueryWrapper.apply("DATE_FORMAT(registration,'%Y')<2020");
            }
        }
        if (xmUser.getUserStatus() != null) {
            xmUserQueryWrapper.eq("user_status", xmUser.getUserStatus());
        }
        return baseMapper.selectPage(xmUserPage, xmUserQueryWrapper);
    }


    @Autowired
    private XmUserMapper xmUserMapper;
    @Override
    public Integer findAllUser() {
        return xmUserMapper.findAllUser();
    }

    @Override
    public Integer findAllUser2() {
        return xmUserMapper.findAllUser2();
    }

    @Autowired
    private XmAuditMapper xmAuditMapper;


    @Autowired
    private XmAuditbusinessMapper xmAuditbusinessMapper;

    @Autowired
    private XmAuditcouriersMapper xmAuditcouriersMapper;

    @Autowired
    private GoodsListMapper goodsListMapper;
    @Override
    public Integer[] countStatus() {
        Integer[] integers = new Integer[9];
        Integer countUser=xmAuditMapper.countUser();
        integers[0]=3;
        integers[1]=2;
        integers[2]=1;
        integers[3]=4;
        integers[4]=1;
//        integers[5]=goodsListMapper.countGoods();
        integers[6]=xmAuditcouriersMapper.countCourier();
        integers[7]=xmAuditbusinessMapper.countSettled();
        integers[8]=countUser;
        return integers;
    }

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public XmUser mmLogin(XmUser xmUser) {
        QueryWrapper<XmUser> xmUserQueryWrapper = new QueryWrapper<>();
        xmUserQueryWrapper.eq("mobile_phone", xmUser.getMobilePhone());
        XmUser user = xmUserMapper.selectOne(xmUserQueryWrapper);
        if(user!=null){
            if(user.getPassword().equals(xmUser.getPassword())){
                user.setLastAccessedTime(LocalDateTime.now());
                xmUserMapper.updateById(user);
                redisTemplate.boundHashOps("user").put(user.getMobilePhone()+"", JSON.toJSONString(user));
                return user;
            }else{
                return null;
            }
        }
        return null;
    }


    @Override
    public XmUser paUser(XmUser xmUser) {
        String string = redisTemplate.boundHashOps("user").get(xmUser.getMobilePhone() + "").toString();
        XmUser user = JSON.parseObject(string, XmUser.class);
        QueryWrapper<XmUser> xmUserQueryWrapper = new QueryWrapper<>();
        QueryWrapper<XmUser> id = xmUserQueryWrapper.eq("id", user.getId());
        XmUser user1 = xmUserMapper.selectOne(id);
        if(user1!=null){
            return user1;
        }else{
            return null;
        }
    }
}